/**
 * 
 */
package NagrywarkaObrazkow;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.LinkedList;


import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;

import sun.util.logging.resources.logging;
import AAInicjalizacja.OsCheck;
import AAInicjalizacja.OsCheck.OSType;
import AAInicjalizacja.init.iInitLinki;

import com.sun.corba.se.impl.orb.ParserTable.TestAcceptor1;


/**
 * @author tom
 *
 */
public abstract class iProgramRecorderObrazow extends JPanel implements iIntrf  {
	
	private LinkedList<BufferedImage> iListaObrazow = null;
	private LinkedList<BufferedImage> iListaPosortowana = null;
	
	private String iPathDefoult = "";
	
	   JFileChooser chooser;
	   String choosertitle="testi";
	   
	public iProgramRecorderObrazow() {
		if (OSType.MacOS == OsCheck.getOperatingSystemType()){
			iPathDefoult = "002-DoFi/SET/MAC/SZUKANE/";
			System.out.println("ścieżka " + iPathDefoult);

		} else if (OSType.Windows == OsCheck.getOperatingSystemType()) {
			iPathDefoult = "002-DoFi/SET/PC/SZUKANE/";
		}
	}
	
	/**
	 * Laduje do LinkedList
	 * @param buf
	 */
	void iIN (BufferedImage buf){
		if (iListaObrazow == null) {
			iListaObrazow = new LinkedList<BufferedImage>();
			iListaPosortowana = new LinkedList<BufferedImage>();
		}
		iListaObrazow.add(buf);
	}
	
	/**
	 * Usuwa zbędne elmenty z tablicy
	 * @throws IOException 
	 */
	void iSortuj() throws IOException{
		if (iListaObrazow == null) return;
		System.out.println("Sortuje elementow " + iListaObrazow.size());
		int iloscElementow = iListaObrazow.size();
		BufferedImage el1=null, el2=null;
		
		
		for (int a = 0; a < iListaObrazow.size(); a++){
			for (int b = a+1; b < iListaObrazow.size(); b++){
				
				
				el1 = iListaObrazow.get(a);
				el2 = iListaObrazow.get(b);
				
				
				if (el1 != null && el2 != null) {
					if (el1.getWidth() != el2.getWidth() || el1.getHeight() != el2.getHeight()) {
						JOptionPane.showConfirmDialog(null, "OBRAZY SĄ NIE ZGONE WIELKOŚCIOWO");
						iListaObrazow = null;
						return;
					}
					
					if (iPorownaj(el1, el2)) {
						iListaObrazow.set(b, null);
						//System.out.println("NULL");
					}; 
				}				
			}
			if (el1 !=null) iListaPosortowana.add(el1);
		}
		
		iZapisz();
	}
	
	
	boolean iPorownaj (BufferedImage el1, BufferedImage el2){
		int tolerancja = 5;
		int a = 0;
		for (int y = 0; y < el1.getHeight(); y++){
			for (int x = 0; x < el1.getWidth(); x++){
				a = Math.abs(el1.getRGB(x, y) - el2.getRGB(x, y));
				if (a >= tolerancja) {
					System.out.println("Tolerancja za wysoka wynosi " + a);
						//iListaObrazow = null;
						//iListaPosortowana = null;
						return false;
				}
			}
		}
		return true;
	}
	
	void iZapisz() throws IOException{
		
		System.out.println("Zapisuje elementow " 
				+ iListaPosortowana.size());
		
		Iterator it = iListaPosortowana.iterator();
		BufferedImage buf = null;
		File iPathFolder = null;
		int licz = 0;
		
		JFileChooser chooser = new JFileChooser();
	    chooser.setCurrentDirectory(
	    		new java.io.File (iInitLinki.iGlobalPathSET + iInitLinki.iKatSys + "SZUKANE/"));
	      System.out.println("test1: " +iInitLinki.iGlobalPathSET + iInitLinki.iKatSys + "SZUKANE/"); 

		chooser.setFileFilter(
				new FileNameExtensionFilter("BMP", "bmp"));

		int result = chooser.showSaveDialog(this);
	    
		if (result == JFileChooser.CANCEL_OPTION){
			iListaObrazow = null;
			iListaPosortowana = null;
			return;
		}
		    
		//File file = chooser.getSelectedFile();
	    if (result == JFileChooser.APPROVE_OPTION) { 
	    	iPathFolder = chooser.getCurrentDirectory();
	      System.out.println("getCurrentDirectory(): " 
	         +  iPathFolder);
	      System.out.println("getSelectedFile() : " 
	         +  chooser.getSelectedFile());
	      }
	    else {
	      System.out.println("No Selection ");
	      }
	    
	    while (it.hasNext()){
	    	buf =  (BufferedImage) it.next();
	    	File outputfile = new File(chooser.getSelectedFile() + "_" + licz++ +".bmp");
		    ImageIO.write(buf, "bmp", outputfile);
		    
	    	iLog(""+outputfile.getAbsoluteFile()); 
	    }
	    
		iListaObrazow = null;
		iListaPosortowana = null;	
	}



	
	

}
